/**
 * Copyright (c) 2011-2014, homeant (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.homeant.common.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <P>
 * http 工具类
 * </P>
 * 
 * @author homeant homeanter@163.com
 * @Data 2017年6月1日 下午10:38:01
 */
public class HttpUtils {
	
	private static final Logger _logger = LoggerFactory.getLogger(HttpUtils.class);
	/**
	 * <p>isFormAjax</p>
	 * <p>
	 * 判断是否来自ajax请求
	 * </p>
	 * @param request
	 * @return
	 * @author homeant homeanter@163.com
	 * @Data 2017年6月1日 下午10:39:03
	 */
	public static boolean isFormAjax( HttpServletRequest request ) {
		return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")) ? true : false;
	}
	
	/**
	 * <p>ajaxStatus</p>
	 * <p>
	 * 设置ajax状态
	 * </p>
	 * @param response
	 * @param status
	 * @param tip
	 * @author homeant homeanter@163.com
	 * @Data 2017年6月1日 下午10:41:21
	 */
	public static void ajaxStatus( HttpServletResponse response, int status, String tip,String charset) {
		try {
			response.setContentType("text/html;charset="+charset);
			response.setStatus(status);
			PrintWriter out = response.getWriter();
			out.print(tip);
			out.flush();
		} catch ( IOException e ) {
			_logger.error(e.toString());
		}
	}
	/**
	 * <p>getQueryString</p>
	 * <p>
	 * 获取当前 URL 包含查询条件
	 * </p>
	 * @param request
	 * @param encode
	 * @return
	 * @throws IOException
	 * @author homeant homeanter@163.com
	 * @Data 2017年6月1日 下午10:43:44
	 */
	public static String getQueryString(HttpServletRequest request, String encode) throws IOException {
		StringBuffer sb = new StringBuffer(request.getRequestURL());
		String query = request.getQueryString();
		if (query != null && query.length() > 0) {
			sb.append("?").append(query);
		}
		return URLEncoder.encode(sb.toString(), encode);
	}
	
	/**
	 * <p>getRequestUrl</p>
	 * <p>
	 * 获取当前完整请求地址
	 * </p>
	 * @param request
	 * @return
	 * @author homeant homeanter@163.com
	 * @Data 2017年6月1日 下午10:45:24
	 */
	public static String getRequestUrl(HttpServletRequest request) {
		StringBuffer url = new StringBuffer(request.getScheme());
		// 请求协议 http,https
		url.append("://");
		url.append(request.getHeader("host"));// 请求服务器
		url.append(request.getRequestURI());// 工程名
		if (request.getQueryString() != null) {
			// 请求参数
			url.append("?").append(request.getQueryString());
		}
		return url.toString();
	}
}
